<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px" >
    <el-form-item label="分类名称:">
        <el-input v-model="dataForm.categoryName" placeholder="分类名称"></el-input>
      </el-form-item>
      <el-form-item label="排序编号:" >
        <el-input v-model="dataForm.sortIndex" placeholder="数字越大，展示越前，默认为0" type="number"></el-input>
      </el-form-item>
      <el-form-item label="分类简介:">
        <el-input v-model="dataForm.categoryDesc" placeholder="分类简介" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="状态:">
          <el-radio v-model="dataForm.isAlive" label="1">启用</el-radio>
          <el-radio v-model="dataForm.isAlive" label="0">禁用</el-radio>
        <!-- <el-input v-model="dataForm.categoryDesc" placeholder="分类简介"></el-input> -->
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-loading="dataList">确定</el-button>
    </span>
  </el-dialog>
</template>
<style>
  /** 去除input输入框样式 */
input[type=number]::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  .number__input {
    border: 1px solid #ccc;
  }
.el-loading-spinner{
      z-index: 10000000 !important;
}
</style>
<script>
  export default {
    data () {
      return {
        visible: false,
        dataList:false,
        dataForm: {
          id: 0,
          categoryName: '',
          categoryDesc: '',
          sortIndex: '',
          gmtCreated: '',
          gmtUpdate: '',
          isAlive: '1',
        },
        dataRule: {
          categoryName: [
            { required: true, message: '分类名称不能为空', trigger: 'blur' },
            {  max: 4, message: '最长4个字符', trigger: 'blur' }
          ],
          sortIndex: [
            { required: true, message: '分类展示排序 从大到小不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/course/category/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.categoryName = data.category.categoryName
                this.dataForm.categoryDesc = data.category.categoryDesc
                this.dataForm.sortIndex = data.category.sortIndex
                this.dataForm.isAlive = data.category.isAlive+''
                console.log(this.dataForm.isAlive)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.dataList = true;
        console.log(this.dataList = true)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/course/category/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'categoryName': this.dataForm.categoryName,
                'categoryDesc': this.dataForm.categoryDesc,
                'sortIndex': this.dataForm.sortIndex,
                'gmtCreated': this.dataForm.gmtCreated,
                'gmtUpdate': this.dataForm.gmtUpdate,
                'isAlive': this.dataForm.isAlive
              })
            }).then(({data}) => {       
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.dataList = false
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                // this.dataList = true
                this.dataList = false;
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }

</script>